﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using SharedLibraries.Constants;
using EntityObject;
using DataAccessObject;
using ThangNM_LIB.FUNC;
using ThangNM_LIB.EO;

namespace SharedLibraries
{
    public class CustomerCache
    {
        //public static Queue<SEO_EXEO> lstQueue_iComment;
        //public static Queue<SEO_EXEO> lstQueue_iVisit;
        //public static Queue<SEO_EXEO> lstQueue_iLike;



        public static String tblTemp_sContent_Change_DB;

        public static List<Category_ZipEO> lstCategory_ZipEO;

        public static List<EmailEO> lstEmailWaitSent;
        public static String strUrlTempMail;
        public static String strUrlLogMail;

        //public static DateTime tTimeOutSitemap;
        //public static String strSitemap;

        //public static DateTime tTimeOutRss;
        //public static String strRss;

        //public static DateTime tTimeOutChart;
        //public static DataSet dsChart;

        public static Boolean IsTimeOut(DateTime tTimeOutApp, object objApp)
        {
            try
            {
                string[] addtime = System.Configuration.ConfigurationManager.AppSettings["CustomerCacheMaxAge"].Split(',');
                switch (addtime[1].ToString().ToUpper())
                {
                    case "SECOND": tTimeOutApp = tTimeOutApp.AddSeconds(Convert.ToDouble(addtime[0])); break;
                    case "MINUTE": tTimeOutApp = tTimeOutApp.AddMinutes(Convert.ToDouble(addtime[0])); break;
                    case "HOUR": tTimeOutApp = tTimeOutApp.AddHours(Convert.ToDouble(addtime[0])); break;
                    case "DAY": tTimeOutApp = tTimeOutApp.AddDays(Convert.ToDouble(addtime[0])); break;
                    case "MONTH": tTimeOutApp = tTimeOutApp.AddMonths(Convert.ToInt32(addtime[0])); break;
                    case "YEAR": tTimeOutApp = tTimeOutApp.AddYears(Convert.ToInt32(addtime[0])); break;
                    default: tTimeOutApp = tTimeOutApp.AddMinutes(2); break;
                }
                if (tTimeOutApp == null || tTimeOutApp < DateTime.Now || objApp == null)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                (new Logs()).WriteLog_To_DB(new LogEO(ex.ToString(), ConstEx.Log_iType.EXCEPTION), false);
                return true;
            }
        }

        public static void AddNewItemEmail(EmailEO _EmailEO)
        {
            if (String.IsNullOrEmpty(strUrlTempMail))
                strUrlTempMail = System.Web.HttpContext.Current.Server.MapPath("~/GlobalResources/thank-you.html");
            if (String.IsNullOrEmpty(strUrlLogMail))
                strUrlLogMail = System.Web.HttpContext.Current.Server.MapPath("~/GlobalResources/emailwait.json");

            if (lstEmailWaitSent == null)
                lstEmailWaitSent = new List<EmailEO>();
            lstEmailWaitSent.Add(_EmailEO);
        }

        public static void ClearCache()
        {
            CustomerCache.lstCategory_ZipEO = null;
            
            CustomerCache.lstEmailWaitSent = null;

            //CustomerCache.tTimeOutSitemap = DateTime.MinValue;
            //CustomerCache.strSitemap = null;

            //CustomerCache.tTimeOutRss = DateTime.MinValue;
            //CustomerCache.strRss = null;

            //CustomerCache.tTimeOutChart = DateTime.MinValue;
            //CustomerCache.dsChart = null;
        }

        #region "Get data in cache"
        public static List<Category_ZipEO> get_lstCategory_ZipEO()
        {
            lstCategory_ZipEO = (new CategoryDAO()).SelectBy_Zip();

            //if (lstCategory_ZipEO != null)
            //{
            //    if (lstCategory_ZipEO.Count <= 0)
            //        lstCategory_ZipEO = CategoryDAO.SelectBy_Zip();
            //}
            //else
            //    lstCategory_ZipEO = CategoryDAO.SelectBy_Zip();
            return lstCategory_ZipEO;
        }

        //public static DataSet get_dsChart()
        //{
        //    TempEO _TempEO = new TempEO();
        //    if (dsChart != null)
        //    {
        //        if (dsChart.Tables.Count < 1 || CustomerCache.IsTimeOut(CustomerCache.tTimeOutChart, CustomerCache.dsChart))
        //        {
        //            CustomerCache.tTimeOutChart = DateTime.Now;
        //            CustomerCache.dsChart = TopicDAO.getDataChart(99);
        //            _TempEO.PK_iTempID = 2;
        //            _TempEO.sContent = Commons.Object2Json(CustomerCache.dsChart);
        //            TempDAO.Update(_TempEO);
        //            Commons.WriteLogError("get Chart");
        //        }
        //    }
        //    else
        //    {
        //        CustomerCache.tTimeOutChart = DateTime.Now;
        //        CustomerCache.dsChart = TopicDAO.getDataChart(99);
        //        _TempEO.PK_iTempID = 2;
        //        _TempEO.sContent = Commons.Object2Json(CustomerCache.dsChart);
        //        TempDAO.Update(_TempEO);
        //        Commons.WriteLogError("get Chart");
        //    }
        //    return dsChart;
        //}
        #endregion
    }
}